package com.right.health.app.ui.user;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.PermissionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.AppUserResponse;
import com.right.health.app.bean.UploadResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.dialog.PhotoDialog;
import com.right.health.app.net.CertificationApi;
import com.right.health.app.net.UploadApi;
import com.right.health.app.utils.ChoosePhotoUtil;
import com.right.health.app.utils.EnterVerifyUtil;

/*
 * Created by hopeliao on 2019-09-27
 * 人工审核认证
 *
 */
public class CertificationByArtificialActivity extends BaseActivity implements View.OnClickListener,
        ChoosePhotoUtil.OnChooseImageCallback, OnHttpListener {

    private static final int HTTP_CODE_UPLOAD_FILE = 1;
    private static final int HTTP_CODE_ADD = 2;

    private ChoosePhotoUtil mChoosePhotoUtil;

    private ImageView mIdCardImage;

    private PhotoDialog mPhotoDialog;

    private EditText mIdcardEt, mNameEt;

    private UploadApi mUploadApi;

    private CertificationApi mCertificationApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.certification_by_artificial);
    }

    @Override
    public void findView() {
        findViewById(R.id.iv_camera).setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mUploadApi = new UploadApi();
        mUploadApi.setListener(this);

        mCertificationApi = new CertificationApi();
        mCertificationApi.setListener(this);

        mIdcardEt = (EditText) findViewById(R.id.et_idcard);
        mNameEt = (EditText) findViewById(R.id.et_name);

        mIdCardImage = (ImageView) findViewById(R.id.iv_idcard);
        mIdCardImage.setOnClickListener(this);

        findViewById(R.id.tv_submit).setOnClickListener(this);

        mChoosePhotoUtil = new ChoosePhotoUtil(this);
        mChoosePhotoUtil.setCallback(this);
        mChoosePhotoUtil.setCropParam(1280, 720);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_idcard:
            case R.id.iv_camera:
                PermissionUtil.with(this)
                        .listener(new PermissionUtil.RequestPermissionListener() {
                            @Override
                            public void onResult(boolean succ, int requestCode) {
                                if (succ) {
                                    if (mPhotoDialog == null) {
                                        mPhotoDialog = new PhotoDialog(CertificationByArtificialActivity.this);
                                        mPhotoDialog.setClickListener(CertificationByArtificialActivity.this);
                                    }
                                    mPhotoDialog.show();
                                }
                            }
                        })
                        .request(
                                Manifest.permission.CAMERA,
                                Manifest.permission.ACCESS_NETWORK_STATE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_PHONE_STATE,
                                Manifest.permission.READ_EXTERNAL_STORAGE);

                break;
            case R.id.tv_choose_camera://相机
                mChoosePhotoUtil.choosePhoto();
                dismissPhotoDialog();
                break;
            case R.id.tv_choose_album: //相册
                mChoosePhotoUtil.chooseAlbum();
                dismissPhotoDialog();
                break;
            case R.id.tv_submit://保存

                send2Net();
                break;
        }
    }

    private void send2Net() {
        if (TextUtils.isEmpty(mImageServerPath)) {
            ToastUtil.show(this, "请上传您的身份证件");
            return;
        }
        String name = mNameEt.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.show(this, R.string.please_enter_name);
            return;
        }
        String idcard = mIdcardEt.getText().toString().trim();
        if(TextUtils.isEmpty(idcard) || !EnterVerifyUtil.verifyCardid(idcard)) {
            ToastUtil.show(this, R.string.please_enter_id_card_number);
            return;
        }
        mCertificationApi.addRequestCode(HTTP_CODE_ADD);
        mCertificationApi.addAuthentication(mImageServerPath, name, idcard, getResources().getString(R.string.def_hospital_code));
    }

    private void dismissPhotoDialog() {
        if (mPhotoDialog != null && mPhotoDialog.isShowing()) {
            mPhotoDialog.dismiss();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mChoosePhotoUtil.onActivityResult(requestCode, resultCode, data);
    }

    private String mImageServerPath;

    @Override
    public void onResult(String filePath) {
        mIdCardImage.setVisibility(View.VISIBLE);
        Glide.with(App.getInstance()).load(filePath).into(mIdCardImage);

        mUploadApi.addRequestCode(HTTP_CODE_UPLOAD_FILE);
        mUploadApi.uploadFile(filePath);
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if(request.requestCode == HTTP_CODE_UPLOAD_FILE) {
                if(response.targetData instanceof UploadResponse) {
                    UploadResponse uploadResponse = response.cast(UploadResponse.class);
                    if(CollectionUtil.isEmpty(uploadResponse.imgPath)) {
                        return;
                    }
                    mImageServerPath = uploadResponse.imgPath.get(0);
                }
            } else if(request.requestCode == HTTP_CODE_ADD) {
                ToastUtil.show(this, "提交认证成功");
                AppUserResponse.AppUserInfo userInfo = App.getInstance().getUser();
                userInfo.verifystatus = "1";
                App.getInstance().refreshUser(userInfo);

                sendBroadcast(BroadCastCode.REFRESH_USER);

                finish();
            }
        }
    }
}
